import React from 'react';
import { connect } from 'react-redux';
import { Form } from 'antd'
import 'styles/setting/workflow/new-workflow.scss'
import 'styles/pay-setting/payment-method/new-payment-method.scss'
import WorkflowBaseSetting from 'containers/setting/new-workflow/setting/base-setting'
import WorkflowConditionSetting from 'containers/setting/new-workflow/setting/condition-setting'
import { messages } from 'share/common'
import { nodeTypes } from "components/simple-flow/node-list";
class NewWorkingSetting extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      nowNode: {},
      approveRuleModalVisible: false,
    };
  }

  componentWillMount() {
    this.setState({
      nowNode: this.props.params.nowNode,
      formFieldList: this.props.params.formFieldList,
      ruleRoutes: this.props.params.ruleRoutes,
      ruleApprovalNodes: this.props.params.ruleApprovalNodes,
      formInfo: this.props.params.formInfo,
      chainInfo: this.props.params.chainInfo,
    })
  }

  componentWillReceiveProps(nextProps) {
    this.setState({
      nowNode: nextProps.params.nowNode,
      formFieldList: nextProps.params.formFieldList,
      ruleRoutes: nextProps.params.ruleRoutes,
      ruleApprovalNodes: nextProps.params.ruleApprovalNodes,
      formInfo: nextProps.params.formInfo,
      chainInfo: nextProps.params.chainInfo,
    })
  }

  handleSave = (e) => {
    this.props.close(e);
  };

  getRender = () => {
    let { nowNode, formFieldList, ruleRoutes, formInfo, chainInfo, ruleApprovalNodes } = this.state;
    if (nowNode.type !== nodeTypes.CONDITION && nowNode) {
      return <WorkflowBaseSetting close={this.onCancel} onSave={this.handleSave}
                                  params={{ formFieldList, nowNode, formInfo, chainInfo }}/>
    } else if (nowNode) {
      return <WorkflowConditionSetting onOpenAdditionModal={() => {this.setState({ approveRuleModalVisible: true })}}
                                       ruleRoutes={ruleRoutes}
                                       ruleApprovalNodes={ruleApprovalNodes}
                                       setOfBooksId={this.props.params.setOfBooksId}
                                       onSave={this.handleSave}
                                       params={{ formFieldList, nowNode, formInfo, chainInfo }}
                                       close={this.onCancel}/>
    }
  };

  onCancel = () => {
    this.props.form.resetFields();
    this.props.close();
  };

  render() {
    return (
      <div className="workflow-base-setting">
        {this.getRender()}
      </div>
    )
  }
}

NewWorkingSetting.PropTypes = {
  onOpenAdditionModal: React.PropTypes.string.func,
};

function mapStateToProps(state) {
  return {
    company: state.login.company,
    user: state.login.user,
    language: state.main.language
  }
}

const WrappedNewWorkingSetting = Form.create()(NewWorkingSetting);

export default connect(mapStateToProps)(WrappedNewWorkingSetting);
